package com.ethink.order.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.IdUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ethink.framework.common.db.PageUtils;
import com.ethink.framework.common.exception.BusinessException;
import com.ethink.order.api.constant.OrderRespCode;
import com.ethink.order.api.enums.TobSaleSettlementOrderOrderStatusEnum;
import com.ethink.order.api.info.TobSaleSettlementDetailOrderInfo;
import com.ethink.order.api.info.TobSaleSettlementOrderInfo;
import com.ethink.order.api.request.TobSaleSettlementOrderAddRequest;
import com.ethink.order.api.request.TobSaleSettlementOrderUpdateRequest;
import com.ethink.order.api.request.TobSaleSettlementPageQueryRequest;
import com.ethink.order.api.response.TobSaleSettlementQueryDetailResponse;
import com.ethink.order.common.entity.TobSaleSettlementOrder;
import com.ethink.order.common.mapper.TobSaleSettlementDetailOrderMapper;
import com.ethink.order.common.mapper.TobSaleSettlementOrderMapper;
import com.ethink.order.manager.SaleChannelRequestManager;
import com.ethink.order.service.TobSaleSettlementDetailOrderService;
import com.ethink.order.service.TobSaleSettlementOrderService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * TOB销售结算单 Service接口实现
 *
 * @author default
 * @date 2022-11-17
 */
@Slf4j
@Service
public class TobSaleSettlementOrderServiceImpl implements TobSaleSettlementOrderService {

    @Autowired
    private TobSaleSettlementOrderMapper tobSaleSettlementOrderMapper;

    @Autowired
    private TobSaleSettlementDetailOrderService tobSaleSettlementDetailOrderService;

    @Autowired
    private TobSaleSettlementDetailOrderMapper tobSaleSettlementDetailOrderMapper;

    @Override
    public void add(TobSaleSettlementOrderAddRequest request) {
        String orderNo = IdUtil.getSnowflakeNextIdStr();
        TobSaleSettlementOrder settlementOrder = new TobSaleSettlementOrder();
        BeanUtils.copyProperties(request, settlementOrder, "item");
        settlementOrder.setOrderNo(orderNo);
        tobSaleSettlementOrderMapper.insert(settlementOrder);
        //保存商品信息
        tobSaleSettlementDetailOrderService.batchInsert(request.getItem(), orderNo,request.getSaleSubChannel());
    }

    @Override
    public Integer delete(Long id) {
        TobSaleSettlementOrder settlementOrder = tobSaleSettlementOrderMapper.selectById(id);
        if (!TobSaleSettlementOrderOrderStatusEnum.S1.getIntValue().equals(settlementOrder.getOrderStatus())) {
            throw new BusinessException(OrderRespCode.SALE_ORDER_DETAIL_STATUS_IS_NOT_MET);
        }
        return tobSaleSettlementOrderMapper.delById(id);
    }

    @Override
    public Integer update(TobSaleSettlementOrderUpdateRequest updateRequest) {
        TobSaleSettlementOrder settlementOrder = tobSaleSettlementOrderMapper.selectById(updateRequest.getId());
        if (TobSaleSettlementOrderOrderStatusEnum.S1.getIntValue().equals(settlementOrder.getOrderStatus())) {
            throw new BusinessException(OrderRespCode.SALE_ORDER_DETAIL_STATUS_IS_NOT_MET);
        }
        return tobSaleSettlementOrderMapper.update(null, new LambdaUpdateWrapper<TobSaleSettlementOrder>()
                .eq(TobSaleSettlementOrder::getId, updateRequest.getId())
                .set(TobSaleSettlementOrder::getSaleChannel, updateRequest.getSaleChannel())
                .set(TobSaleSettlementOrder::getSaleSubChannel, updateRequest.getSaleSubChannel()));
    }

    @Override
    public Page<TobSaleSettlementOrderInfo> pageQuery(TobSaleSettlementPageQueryRequest request) {
        SaleChannelRequestManager.fill(request, 0, 1, 2);
        return tobSaleSettlementOrderMapper.pageQuery(PageUtils.buildPage(request), request);
    }

    @Override
    public TobSaleSettlementQueryDetailResponse queryDetail(Long id) {
        TobSaleSettlementQueryDetailResponse response = new TobSaleSettlementQueryDetailResponse();
        TobSaleSettlementOrder settlementOrder = tobSaleSettlementOrderMapper.selectById(id);
        if (ObjectUtil.isEmpty(settlementOrder)) {
            return response;
        }
        TobSaleSettlementOrderInfo main = BeanUtil.copyProperties(settlementOrder, TobSaleSettlementOrderInfo.class);
        response.setMain(main);
        List<TobSaleSettlementDetailOrderInfo> item = tobSaleSettlementDetailOrderMapper.selectListByOrderNo(settlementOrder.getOrderNo());
        response.setItem(item);
        return response;
    }

    @Override
    public Integer matchConfirm(Long id) {
        return tobSaleSettlementOrderMapper.update(null, new LambdaUpdateWrapper<TobSaleSettlementOrder>()
                .eq(TobSaleSettlementOrder::getId, id)
                .set(TobSaleSettlementOrder::getOrderStatus,TobSaleSettlementOrderOrderStatusEnum.S2.getIntValue()));
    }
}
